package org.dut.community.config;

import org.dut.community.quartz.PostScoreRefreshJob;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;

@Configuration
public class QuartzConfig {
    @Bean
    public JobDetailFactoryBean jobDetailFactoryBean() {
        // 创建JobDetailFactoryBean实例，用于配置和创建JobDetail对象
        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
        // 设置定时任务的具体实现类（PostScoreRefreshJob）
        factoryBean.setJobClass(PostScoreRefreshJob.class);
        // 设置任务名称
        factoryBean.setName("PostScoreRefreshJob");
        // 设置任务所属的分组
        factoryBean.setGroup("communityJobGroup");
        // 设置任务持久化，即使没有触发器关联也会保留在Quartz的JobStore中
        factoryBean.setDurability(true);
        // 设置任务可恢复，当Quartz服务重启后，会重新执行中断的任务
        factoryBean.setRequestsRecovery(true);
        return factoryBean;
    }
    
    @Bean
    // 注入上面创建的JobDetail对象作为参数
    public SimpleTriggerFactoryBean simpleTriggerFactoryBean(JobDetail postScoreRefreshJob) {
        // 创建SimpleTriggerFactoryBean实例，用于配置和创建触发器
        SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
        // 设置触发器关联的JobDetail
        factoryBean.setJobDetail(postScoreRefreshJob);
        // 设置触发器名称
        factoryBean.setName("PostScoreRefreshTrigger");
        // 设置触发器所属的分组
        factoryBean.setGroup("CommunityTriggerGroup");
        // 设置任务执行间隔时间（单位：毫秒），这里配置为5分钟执行一次
        factoryBean.setRepeatInterval(1000*60*5);
        // 设置JobDataMap，可用于在任务执行时传递数据
        factoryBean.setJobDataMap(new JobDataMap());
        return factoryBean;
    }
}